package com.loto.insurance.oem.web.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.loto.insurance.oem.web.authority.OEMAuthority;
import com.loto.insurance.oem.web.entity.OrderInfo;
import com.loto.insurance.oem.web.entity.OrderInfoExt;
import com.loto.insurance.oem.web.entity.Pager;
import com.loto.insurance.oem.web.entity.PolicyInfo;
import com.loto.insurance.oem.web.entity.SessionUser;
import com.loto.insurance.oem.web.enumeration.AuthorityRoleEnum;
import com.loto.insurance.oem.web.enumeration.InsTypeEnum;
import com.loto.insurance.oem.web.enumeration.MenuEnum;
import com.loto.insurance.oem.web.enumeration.OrderStatusEnum;
import com.loto.insurance.oem.web.enumeration.PolicyStatusEnum;
import com.loto.insurance.oem.web.service.OrderInfoService;
import com.loto.insurance.oem.web.service.PolicyInfoService;
import com.loto.utils.DateUtils;
import com.loto.utils.StringUtils;

@Controller
@SessionAttributes("oem_user")
public class OrderController {
  @Autowired
  OrderInfoService orderInfoService;

  @Autowired
  PolicyInfoService policyInfoService;

  @OEMAuthority
  @RequestMapping(value = "/order/list", method = RequestMethod.GET)
  public String orderList(
	  @RequestParam(value = "orderNo", required = false) String orderNo,
	  @RequestParam(value = "orderBegin", required = false) String orderBegin,
	  @RequestParam(value = "orderEnd", required = false) String orderEnd,
	  @RequestParam(value = "orderStatus", required = false, defaultValue = "-1") int orderStatus,
	  @RequestParam(value = "orderCompany", required = false) String orderCompany,
	  @RequestParam(value = "orderType", required = false, defaultValue = "-1") int orderType,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (orderNo != null && !orderNo.isEmpty())
	  params.put("orderNo", orderNo);
	if (orderBegin != null && orderEnd != null
	    && DateUtils.isDate(orderBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(orderEnd, "yyyy-MM-dd")) {
	  params.put("orderBegin", orderBegin);
	  params.put("orderEnd", orderEnd);
	}
	if (orderStatus > -1)
	  params.put("orderStatus", orderStatus);
	if (orderCompany != null && !orderCompany.isEmpty())
	  params.put("orderCompany", orderCompany);
	if (orderType > -1)
	  params.put("orderType", orderType);

	Pager pager = new Pager();
	if (params.size() > 0) {
	  pager.setParams(StringUtils.paramJoin(params));
	}
	params.put("custId", sessionUser.getUserId());
	model.addAllAttributes(params);

	if (params.size() == 1) {
	  Calendar now = Calendar.getInstance();
	  String today = DateUtils.dateToNormalString(now.getTime());
	  params.put("orderBegin", today);
	  params.put("orderEnd", today);
	}
	// int pageSize = 1;
	// pager.setPageSize(pageSize);
	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	ArrayList<OrderInfo> orderList = orderInfoService
	    .findAllOrdersByCust(params);

	pager.setRowCount(orderInfoService.findAllOrdersByCustCount(params));

	ArrayList<String> companyList = orderInfoService
	    .findInsCompanyByCust(sessionUser.getUserId());

	model.addAttribute("orderStatusList", OrderStatusEnum.values());
	model.addAttribute("insTypeList", InsTypeEnum.values());
	model.addAttribute("companyList", companyList);
	model.addAttribute("pager", pager);
	model.addAttribute("orderList", orderList);

	model.addAttribute("pageId", MenuEnum.ORDER_LIST.getIndex());
	return "order/order_list";
  }

  @OEMAuthority
  @RequestMapping(value = "/order/detail", method = RequestMethod.GET)
  public String orderDetail(
	  @RequestParam(value = "id", required = true) long orderId, Model model,
	  @ModelAttribute("oem_user") SessionUser sessionUser) {

	OrderInfo orderInfo = orderInfoService.findOrderInfoById(orderId,
	    sessionUser.getUserId());

	OrderInfoExt orderInfoExt = orderInfoService.findOrderInfoExtById(orderId);

	if (orderInfo != null && orderInfoExt != null) {
	  model.addAttribute("order", orderInfo);
	  model.addAttribute("orderExt", orderInfoExt);
	}
	return "order/order_detail";
  }

  @OEMAuthority
  @RequestMapping(value = "/order/detailview")
  public String orderDetailView(
	  @RequestParam(value = "insType", required = true) int insType,
	  @RequestParam(value = "orderId", required = true) long orderId,
	  Model model) {
	if (insType == InsTypeEnum.AIR.getIndex()) {
	  model.addAttribute("policyInfoList",
		  policyInfoService.getPolicyInfoAirById(orderId));
	  return "ctrl/order_view_air";
	} else if (insType == InsTypeEnum.TRAVEL.getIndex()) {
	  model.addAttribute("policyInfoList",
		  policyInfoService.getPolicyInfoTravelById(orderId));
	  return "ctrl/order_view_travel";
	}
	return "ctrl/order_view_air";
  }

  @OEMAuthority
  @RequestMapping(value = "/policy/list", method = RequestMethod.GET)
  public String policyList(
	  @RequestParam(value = "policyNo", required = false) String policyNo,
	  @RequestParam(value = "policyName", required = false) String policyName,
	  @RequestParam(value = "credentialNo", required = false) String credentialNo,
	  @RequestParam(value = "policyBegin", required = false) String policyBegin,
	  @RequestParam(value = "policyEnd", required = false) String policyEnd,
	  @RequestParam(value = "policyType", required = false, defaultValue = "-1") int policyType,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (policyNo != null && !policyNo.isEmpty())
	  params.put("policyNo", policyNo);
	if (policyName != null && !policyName.isEmpty())
	  params.put("policyName", policyName);
	if (credentialNo != null && !credentialNo.isEmpty())
	  params.put("credentialNo", credentialNo);
	if (policyBegin != null && policyEnd != null
	    && DateUtils.isDate(policyBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(policyEnd, "yyyy-MM-dd")) {
	  params.put("policyBegin", policyBegin);
	  params.put("policyEnd", policyEnd);
	}
	if (policyType > -1)
	  params.put("policyType", policyType);

	Pager pager = new Pager();
	if (params.size() > 0) {
	  pager.setParams(StringUtils.paramJoin(params));
	}
	params.put("custId", sessionUser.getUserId());
	model.addAllAttributes(params);

	if (params.size() == 1) {
	  Calendar now = Calendar.getInstance();
	  String today = DateUtils.dateToNormalString(now.getTime());
	  params.put("orderBegin", today);
	  params.put("orderEnd", DateUtils.addMonths(now.getTime(), 1));
	}

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<PolicyInfo> policyList = policyInfoService
	    .findAllPoliciesByCust(params);

	pager.setRowCount(policyInfoService.findAllPoliciesByCustCount(params));

	model.addAttribute("insTypeList", InsTypeEnum.values());
	model.addAttribute("pager", pager);
	model.addAttribute("policyList", policyList);

	model.addAttribute("pageId", MenuEnum.POLICY_LIST.getIndex());
	return "order/policy_list";
  }

  @OEMAuthority
  @RequestMapping(value = "/policy/problem", method = RequestMethod.GET)
  public String policyProblem(
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	Pager pager = new Pager();
	params.put("custId", sessionUser.getUserId());

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<PolicyInfo> policyList = policyInfoService
	    .findProblemPoliciesByCust(params);

	pager.setRowCount(policyInfoService
	    .findProblemPoliciesByCustCount(sessionUser.getUserId()));

	model.addAttribute("pager", pager);
	model.addAttribute("policyList", policyList);

	model.addAttribute("pageId", MenuEnum.POLICY_PROBLEM.getIndex());
	return "order/problem_policy_list";
  }

  @OEMAuthority
  @RequestMapping(value = "/order/unpaid", method = RequestMethod.GET)
  public String orderList(
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	Pager pager = new Pager();
	params.put("custId", sessionUser.getUserId());
	params.put("orderStatus", OrderStatusEnum.UNPAY.getIndex());
	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	ArrayList<OrderInfo> orderList = orderInfoService
	    .findAllOrdersByCust(params);

	pager.setRowCount(orderInfoService.findAllOrdersByCustCount(params));

	model.addAttribute("pager", pager);
	model.addAttribute("orderList", orderList);

	model.addAttribute("pageId", MenuEnum.ORDER_UNPAID.getIndex());
	return "order/unpaid_order_list";
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.USERS)
  @RequestMapping(value = "/trade/orderlist", method = RequestMethod.GET)
  public String tradeorderList(
	  @RequestParam(value = "custName", required = false) String custName,
	  @RequestParam(value = "orderNo", required = false) String orderNo,
	  @RequestParam(value = "orderBegin", required = false) String orderBegin,
	  @RequestParam(value = "orderEnd", required = false) String orderEnd,
	  @RequestParam(value = "orderStatus", required = false, defaultValue = "-1") int orderStatus,
	  @RequestParam(value = "orderCompany", required = false) String orderCompany,
	  @RequestParam(value = "orderType", required = false, defaultValue = "-1") int orderType,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (custName != null && !custName.isEmpty())
	  params.put("custName", custName);
	if (orderNo != null && !orderNo.isEmpty())
	  params.put("orderNo", orderNo);
	if (orderBegin != null && orderEnd != null
	    && DateUtils.isDate(orderBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(orderEnd, "yyyy-MM-dd")) {
	  params.put("orderBegin", orderBegin);
	  params.put("orderEnd", orderEnd);
	}
	if (orderStatus > -1)
	  params.put("orderStatus", orderStatus);
	if (orderCompany != null && !orderCompany.isEmpty())
	  params.put("orderCompany", orderCompany);
	if (orderType > -1)
	  params.put("orderType", orderType);

	Pager pager = new Pager();
	if (params.size() > 0) {
	  pager.setParams(StringUtils.paramJoin(params));
	}
	model.addAllAttributes(params);

	if (params.size() == 0) {
	  Calendar now = Calendar.getInstance();
	  String today = DateUtils.dateToNormalString(now.getTime());
	  params.put("orderBegin", today);
	  params.put("orderEnd", today);
	}
	// int pageSize = 1;
	// pager.setPageSize(pageSize);
	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	ArrayList<OrderInfo> orderList = orderInfoService.findAllOrders(params);

	pager.setRowCount(orderInfoService.findAllOrdersCount(params));

	ArrayList<String> companyList = orderInfoService.findInsCompany();

	model.addAttribute("orderStatusList", OrderStatusEnum.values());
	model.addAttribute("insTypeList", InsTypeEnum.values());
	model.addAttribute("companyList", companyList);
	model.addAttribute("pager", pager);
	model.addAttribute("orderList", orderList);

	model.addAttribute("pageId", MenuEnum.TRADE_ORDER_LIST.getIndex());
	return "trade/order_list";
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.USERS)
  @RequestMapping(value = "/trade/policylist", method = RequestMethod.GET)
  public String tradepolicyList(
	  @RequestParam(value = "custName", required = false) String custName,
	  @RequestParam(value = "policyNo", required = false) String policyNo,
	  @RequestParam(value = "policyName", required = false) String policyName,
	  @RequestParam(value = "credentialNo", required = false) String credentialNo,
	  @RequestParam(value = "policyBegin", required = false) String policyBegin,
	  @RequestParam(value = "policyEnd", required = false) String policyEnd,
	  @RequestParam(value = "policyStatus", required = false, defaultValue = "-1") int policyStatus,
	  @RequestParam(value = "policyType", required = false, defaultValue = "-1") int policyType,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model, @ModelAttribute("oem_user") SessionUser sessionUser) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (custName != null && !custName.isEmpty())
	  params.put("custName", custName);
	if (policyNo != null && !policyNo.isEmpty())
	  params.put("policyNo", policyNo);
	if (policyName != null && !policyName.isEmpty())
	  params.put("policyName", policyName);
	if (credentialNo != null && !credentialNo.isEmpty())
	  params.put("credentialNo", credentialNo);
	if (policyBegin != null && policyEnd != null
	    && DateUtils.isDate(policyBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(policyEnd, "yyyy-MM-dd")) {
	  params.put("policyBegin", policyBegin);
	  params.put("policyEnd", policyEnd);
	}
	if (policyType > -1)
	  params.put("policyType", policyType);
	if (policyStatus > -1)
	  params.put("policyStatus", policyStatus);

	Pager pager = new Pager();
	if (params.size() > 0) {
	  pager.setParams(StringUtils.paramJoin(params));
	}
	params.put("custId", sessionUser.getUserId());
	model.addAllAttributes(params);

	if (params.size() == 1) {
	  Calendar now = Calendar.getInstance();
	  String today = DateUtils.dateToNormalString(now.getTime());
	  params.put("orderBegin", today);
	  params.put("orderEnd", DateUtils.addMonths(now.getTime(), 1));
	}

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<PolicyInfo> policyList = policyInfoService.findAllPolicies(params);

	pager.setRowCount(policyInfoService.findAllPoliciesCount(params));

	model.addAttribute("policyStatusList", PolicyStatusEnum.values());
	model.addAttribute("insTypeList", InsTypeEnum.values());
	model.addAttribute("pager", pager);
	model.addAttribute("policyList", policyList);

	model.addAttribute("pageId", MenuEnum.TRADE_POLICY_LIST.getIndex());
	return "trade/policy_list";
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.USERS)
  @RequestMapping(value = "/trade/orderdetail", method = RequestMethod.GET)
  public String tradeOrderDetail(
	  @RequestParam(value = "id", required = true) long orderId, Model model) {

	OrderInfo orderInfo = orderInfoService.findOrderInfoById(orderId, 0);

	OrderInfoExt orderInfoExt = orderInfoService.findOrderInfoExtById(orderId);

	if (orderInfo != null && orderInfoExt != null) {
	  model.addAttribute("order", orderInfo);
	  model.addAttribute("orderExt", orderInfoExt);
	}
	return "trade/order_detail";
  }
}
